<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
/**
 * 广告管理
 * Class Ads
 * @package app\admin\controller
 */
class Label extends AdminBase
{
    public function index()
    {
        $list = Db::table('lsj_label')->select();
        return $this->fetch('index',['list'=>$list]);
    }

    public function add()
    {
        if($this->request->isPost()){
            $data['name'] = input('post.label_name');
            $data['created'] = date("Y-m-d H:i:s",time());

            $insert_result = Db::table('lsj_label')->insert($data);
            if($insert_result){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }

        }else{
            return $this->fetch('add');
        }
    }

    public function edit()
    {
        if($this->request->isPost()){
            $data['name'] = input('label_name');
            $data['id'] = input('id');

            $update_result = Db::table('lsj_label')->update($data);
            if($update_result){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }

        }else{
            $id = input('id');
            if(empty($id)){
                $this->error('请选择要操作的id');
            }
            $list = Db::table('lsj_label')->where("id={$id}")->find();
            return $this->fetch('edit',['list'=>$list]);
        }
    }

}